import {
  getActivityPhotoList
} from '@/api/salon'

let col1H = 0;
let col2H = 0;

Page({

  data: {
    imgUri: getApp().globalData.imgUri,
    cdn: getApp().globalData.cdn,
    scrollH: 0,
    imgWidth: 0,
    loadingCount: 0,
    images: [],
    col1: [],
    col2: [],
    activityId:1,
  },

  onLoad: function (options) {
    let res = wx.getWindowInfo()

    if(options.id){
      this.setData({
        activityId: options.id,
      });
      let ww = res.windowWidth;
      let wh = res.windowHeight;
      let imgWidth = ww * 0.48;
      let scrollH = wh;
      this.setData({
        scrollH: scrollH,
        imgWidth: imgWidth
      });

      getActivityPhotoList(this.data.activityId).then(res=>{
        if(res.code == 200){
          var data = res.data;
          console.log(data);
          this.loadImages(data);
        }
      });
    }

  },

  onImageLoad: function (e) {
    let imageId = e.currentTarget.id;
    let oImgW = e.detail.width; //图片原始宽度
    let oImgH = e.detail.height; //图片原始高度
    let imgWidth = this.data.imgWidth; //图片设置的宽度
    let scale = imgWidth / oImgW; //比例计算
    let imgHeight = oImgH * scale; //自适应高度

    let images = this.data.images;
    let imageObj = null;

    for (let i = 0; i < images.length; i++) {
      let img = images[i];
      if (img.id === imageId) {
        imageObj = img;
        break;
      }
    }

    imageObj.height = imgHeight;

    let loadingCount = this.data.loadingCount - 1;
    let col1 = this.data.col1;
    let col2 = this.data.col2;

    if (col1H <= col2H) {
      col1H += imgHeight;
      col1.push(imageObj);
    } else {
      col2H += imgHeight;
      col2.push(imageObj);
    }

    let data = {
      loadingCount: loadingCount,
      col1: col1,
      col2: col2
    };

    if (!loadingCount) {
      data.images = [];
    }

    this.setData(data);
  },

  loadImages: function (data) {
    let imgUri = this.data.imgUri;
    let images = [];
    for(var i=0;i<data.length;i++){
      images.push({pic:this.data.cdn+data[i].activityPhoto,height:0});
    }
    /*
    images = [{
        pic: imgUri + "/salon/f1.png",
        height: 0
      },
      {
        pic: imgUri + "/salon/f2.png",
        height: 0
      },
      {
        pic: imgUri + "/salon/f3.png",
        height: 0
      },
      {
        pic: imgUri + "/salon/f4.png",
        height: 0
      },
      {
        pic: imgUri + "/salon/f5.png",
        height: 0
      },
      {
        pic: imgUri + "/salon/f6.png",
        height: 0
      },
    ];
*/
    let baseId = "img-" + (+new Date());

    for (let i = 0; i < images.length; i++) {
      images[i].id = baseId + "-" + i;
    }

    this.setData({
      loadingCount: images.length,
      images: images
    });
  },

  findThisImage(e){
    let { src } = e.currentTarget.dataset
    wx.previewImage({
      urls: [src],
    })
  },
  onShareAppMessage() {
    return {
      title: getApp().globalData.share_title,
      path: getApp().globalData.share_url,
      imageUrl: getApp().globalData.share_image,
    }
  }
})